/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle.commandhandles.customcommands;

import de.altitude.cimuelle.Config;
import de.altitude.cimuelle.MessageBundle;
import de.altitude.cimuelle.SendMail;
import de.altitudecustomcommands.Player;
import de.altitudecustomcommands.exceptions.ServerNotFoundException;
import de.altitudecustomcommands.serverInformations;
import de.altitudecustomcommands.virtualServer;
import org.apache.log4j.Logger;

import javax.mail.MessagingException;

import static de.altitude.cimuelle.utils.ServerOperations.sendServerWhisper;

/**
 * Class description:
 * processes the specialCommands from customJson
 * User: cybernaut
 * Date: 08.10.11
 * Time: 16:14
 */
public class ReportUser {
    private final static Logger LOGGER = Logger.getLogger("ReportUser");
    private final serverInformations server;

    public ReportUser(serverInformations server) {
        this.server = server;
    }

    public void reportUser(String port, Player playerSrc, String playerName, String reason) {
        String reportUser = "reportUser";
        String reportBody = "Hello,\n" + playerSrc.getName() + " had used the reporting Feature.\n";
        virtualServer server;
        try {
            server = this.server.getServerByPort(Integer.parseInt(port));
        } catch (ServerNotFoundException e) {
            LOGGER.error("Server not Found " + e.getLocalizedMessage());
            server = this.server.getServer(0);
        }
        reportUser += "To Report User: " + playerName + "\n";
        reportBody += "Reason:\n" + reason + "\n";
        reportBody += "\nInformation:\n";
        reportBody += server.toString();
        reportBody += "\n\nUser Information:\n";
        reportBody += playerSrc.toString();
        reportBody += "\n\nSincerly,\nYour Altitude Server";

        try {
            Config config = Config.getInstance();
            String recipient = config.getConfigByPort(port, reportUser + "_receiver", "root@localhost");
            String subject = config.getConfigByPort(port, reportUser + "_subject", server.getName() + "_" + reportUser + "_by_" + playerSrc.getName());
            String body = config.getConfigByPort(port, reportUser + "_body", reportBody);
            SendMail.postMail(recipient, subject, body, config.getConfigByPort(port, reportUser + "_sender", "root@localhost"));
            sendServerWhisper(port, playerSrc.getName(), MessageBundle.getString("wmailReportUser"));
        } catch (MessagingException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
    }
}
